package com.turing.data.scripts.contract.filter

import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import org.springframework.util.CollectionUtils
import com.turing.common.util.FileUtils

class ValuationMethod_30197_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        def REGEX_VALUATION_1 = '估值方法:'
        def REGEX_VALUATION_2 = '估值方法'
        def REGEX_VALUATION_3 = '方法估值'
        def REGEX_VALUATION_4 = '方法估值:'
        def REGEX_VALUATION_5 = '方法估值'
        def REGEX_VALUATION_6 = '、估值方法'
        def REGEX_NUMBER_1 = '（1）'
        def REGEX_NUMBER_2 = '1、'
        def REGEX_NUMBER_3 = '1.'
        def REGEX_NUMBER_4 = '（I）'
        def REGEX_NUMBER_5 = '（l）'

        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs!=null && resultVOs.size()>0) {

            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
//                    return  resultVOs
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                while( expandOffset < 150 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,end)
                    if( ( expandString.contains(REGEX_VALUATION_6)) ){
                        def startOffset = 0
                        def endOffset = 0
                        boolean needIndexFirst = true
                        if(expandString.contains(REGEX_NUMBER_1)){
                            def index = text.indexOf(REGEX_NUMBER_1)
                            startOffset = index - expandOffset
                            endOffset = - ( text.length() + expandOffset - index )
                            needIndexFirst = false
                        }else if(expandString.contains(REGEX_NUMBER_2)){
                            def index = text.indexOf(REGEX_NUMBER_2)
                            startOffset = index - expandOffset
                            endOffset = - ( text.length() + expandOffset - index )
                            needIndexFirst = false
                        }else if(expandString.contains(REGEX_NUMBER_3)){
                            def index = text.indexOf(REGEX_NUMBER_3)
                            startOffset = index - expandOffset
                            endOffset = - ( text.length() + expandOffset - index )
                            needIndexFirst = false
                        }else if(expandString.contains(REGEX_NUMBER_4)){
                            def index = text.indexOf(REGEX_NUMBER_4)
                            startOffset = index - expandOffset
                            endOffset = - ( text.length() + expandOffset - index )
                            needIndexFirst = false
                        }else if(expandString.contains(REGEX_NUMBER_5)){
                            def index = text.indexOf(REGEX_NUMBER_5)
                            startOffset = index - expandOffset
                            endOffset = - ( text.length() + expandOffset - index )
                            needIndexFirst = false
                        }
                        for (int j = 2; j < 1000; j++) {
                            def REGEX_1 = '。' + j + '.'
                            def REGEX_2 = '；' + j + '.'
                            def REGEX_3 = '。' + j + '、'
                            def REGEX_4 = '。（' + j + '）'
                            def REGEX_5 = '。（' + 4
                            def expandOffset0 = 1
                            def replicationEndOffset = endOffset
                            while( expandOffset0 < 800 && rawText.length() > end + expandOffset0 + endOffset ){
                                def expandStr = ""
                                try {
                                    expandStr = rawText.substring(start+endOffset,end+expandOffset0+endOffset)
                                } catch (Exception e) {
                                    e.printStackTrace()
                                }

                                if (needIndexFirst) {
                                    if(expandStr.contains(REGEX_NUMBER_1)){
                                        def index = expandStr.indexOf(REGEX_NUMBER_1)
                                        startOffset = index - expandOffset
                                        needIndexFirst = false
                                    }else if(expandStr.contains(REGEX_NUMBER_2)){
                                        def index = expandStr.indexOf(REGEX_NUMBER_2)
                                        startOffset = index - expandOffset
                                        needIndexFirst = false
                                    }else if(expandStr.contains(REGEX_NUMBER_3)){
                                        def index = expandStr.indexOf(REGEX_NUMBER_3)
                                        startOffset = index - expandOffset
                                        needIndexFirst = false
                                    }else if(expandString.contains(REGEX_NUMBER_4)){
                                        def index = text.indexOf(REGEX_NUMBER_4)
                                        startOffset = index - expandOffset
                                        endOffset = - ( text.length() + expandOffset - index )
                                        needIndexFirst = false
                                    }else if(expandString.contains(REGEX_NUMBER_5)){
                                        def index = text.indexOf(REGEX_NUMBER_5)
                                        startOffset = index - expandOffset
                                        endOffset = - ( text.length() + expandOffset - index )
                                        needIndexFirst = false
                                    }
                                }
                                if( expandStr =~ REGEX_1 || expandStr =~ REGEX_2
                                        || expandStr =~ REGEX_3 || expandStr =~ REGEX_4 || expandStr =~ REGEX_5 ){
                                    endOffset += expandOffset0
                                    break
                                }
                                expandOffset0 ++
                            }
                            if(replicationEndOffset == endOffset){
                                break
                            }
                        }

                        def REGEX_STOP = "。"
                        def expand = 1
                        while( expand < 250 && rawText.length() > end + expand + endOffset ){
                            def expandStr = rawText.substring(end+endOffset,end+expand+endOffset)
                            if( expandStr =~ REGEX_STOP ){
                                endOffset += expand
                                break
                            }
                            expand ++
                        }

                        def REGEX_EXACT_STOP = '规定估值'
                        def exact_expand = 1
                        while( exact_expand < 2000 && rawText.length() > end + exact_expand + endOffset ){
                            def expandStr = rawText.substring(end+endOffset,end+exact_expand+endOffset)
                            if( expandStr =~ REGEX_EXACT_STOP ){
                                endOffset += exact_expand

                                break
                            }
                            exact_expand ++
                        }

                        def JSONTextMap = dependParams.JSONTextMap
                        def fileTextStr = ""
                        fileTextStr = JSONTextMap.get(drawResultVO.childTaskSn)
                        TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                        objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)
                        objectNew.text = rawText.substring( start + startOffset, end + endOffset )
                        objectNew.businessValue = rawText.substring( start + startOffset, end + endOffset )
                        trueValueSet.add(objectNew)
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }

            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                while( expandOffset < 400 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,end)
                    if( expandString.contains(REGEX_VALUATION_4) ){
                        def startOffset = 0
                        def endOffset = 0
                        boolean needIndexFirst = true
                        if(expandString.contains(REGEX_NUMBER_1)){
                            def index = expandString.indexOf(REGEX_NUMBER_1)
                            startOffset = index - expandOffset
                            endOffset = - ( expandString.length() + expandOffset - index )
                            needIndexFirst = false
                        }else if(expandString.contains(REGEX_NUMBER_2)){
                            def index = expandString.indexOf(REGEX_NUMBER_2)
                            startOffset = index - expandOffset
                            endOffset = - ( expandString.length() + expandOffset - index )
                            needIndexFirst = false
                        }else if(expandString.contains(REGEX_NUMBER_3)){
                            def index = expandString.indexOf(REGEX_NUMBER_3)
                            startOffset = index - expandOffset
                            endOffset = - ( expandString.length() + expandOffset - index )
                            needIndexFirst = false
                        }else if(expandString.contains(REGEX_NUMBER_4)){
                            def index = text.indexOf(REGEX_NUMBER_4)
                            startOffset = index - expandOffset
                            endOffset = - ( text.length() + expandOffset - index )
                            needIndexFirst = false
                        }else if(expandString.contains(REGEX_NUMBER_5)){
                            def index = text.indexOf(REGEX_NUMBER_5)
                            startOffset = index - expandOffset
                            endOffset = - ( text.length() + expandOffset - index )
                            needIndexFirst = false
                        }
                        for (int j = 2; j < 1000; j++) {
                            def REGEX_1 = '。' + j + '.'
                            def REGEX_2 = '；' + j + '.'
                            def REGEX_3 = '。' + j + '、'
                            def REGEX_4 = '（' + j + '）'
//                            def REGEX_5 = '（' + j + ''
                            def expandOffset0 = 1
                            def replicationEndOffset = endOffset
                            while( expandOffset0 < 350 && rawText.length() > end + expandOffset0 + endOffset ){
                                def expandStr = ""
                                try {
                                    expandStr = rawText.substring(start,end+expandOffset0+endOffset)
                                } catch (Exception e) {
                                    e.printStackTrace()
                                }
                                if (needIndexFirst) {
                                    if(expandStr.contains(REGEX_NUMBER_1)){
                                        def index = expandStr.indexOf(REGEX_NUMBER_1)
                                        startOffset = index - expandOffset
                                        needIndexFirst = false
                                    }else if(expandStr.contains(REGEX_NUMBER_2)){
                                        def index = expandStr.indexOf(REGEX_NUMBER_2)
                                        startOffset = index - expandOffset
                                        needIndexFirst = false
                                    }else if(expandStr.contains(REGEX_NUMBER_3)){
                                        def index = expandStr.indexOf(REGEX_NUMBER_3)
                                        startOffset = index - expandOffset
                                        needIndexFirst = false
                                    }else if(expandString.contains(REGEX_NUMBER_4)){
                                        def index = text.indexOf(REGEX_NUMBER_4)
                                        startOffset = index - expandOffset
                                        endOffset = - ( text.length() + expandOffset - index )
                                        needIndexFirst = false
                                    }else if(expandString.contains(REGEX_NUMBER_5)){
                                        def index = text.indexOf(REGEX_NUMBER_5)
                                        startOffset = index - expandOffset
                                        endOffset = - ( text.length() + expandOffset - index )
                                        needIndexFirst = false
                                    }
                                }
                                if( expandStr =~ REGEX_1 || expandStr =~ REGEX_2 || expandStr =~ REGEX_3 || expandStr =~ REGEX_4 ){
                                    endOffset += expandOffset0
                                    break
                                }
                                expandOffset0 ++
                            }
                            if(replicationEndOffset == endOffset){
                                break
                            }
                        }

                        def REGEX_STOP = "。"
                        def expand = 1
                        while( expand < 400 && rawText.length() > end + expand + endOffset ){
                            def expandStr = rawText.substring(end+endOffset,end+expand+endOffset)
                            if( expandStr =~ REGEX_STOP ){
                                endOffset += expand
                                break
                            }
                            expand ++
                        }

                        def REGEX_EXACT_STOP = '规定估值'
                        def exact_expand = 1
                        while( exact_expand < 2000 && rawText.length() > end + exact_expand + endOffset ){
                            def expandStr = rawText.substring(end+endOffset,end+exact_expand+endOffset)
                            if( expandStr =~ REGEX_EXACT_STOP ){
                                endOffset += exact_expand

                                break
                            }
                            exact_expand ++
                        }


                        def JSONTextMap = dependParams.JSONTextMap
                        def fileTextStr = ""
                        fileTextStr = JSONTextMap.get(drawResultVO.childTaskSn)
                        TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                        objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)
                        objectNew.text = rawText.substring( start + startOffset, end + endOffset )
                        objectNew.businessValue = rawText.substring( start + startOffset, end + endOffset )
                        trueValueSet.add(objectNew)
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }

            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                while( expandOffset < 400 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,end)
                    if( (expandString.contains(REGEX_VALUATION_1) || expandString.contains(REGEX_VALUATION_2) ||
                            expandString.contains(REGEX_VALUATION_3) || expandString.contains(REGEX_VALUATION_5)) ){
                        def startOffset = 0
                        def endOffset = 0

                        boolean needIndexFirst = true
                        if(expandString.contains(REGEX_NUMBER_1)){
                            def index = expandString.indexOf(REGEX_NUMBER_1)
                            startOffset = index - expandOffset
                            endOffset = - ( expandString.length() + expandOffset - index )
                            needIndexFirst = false
                        }else if(expandString.contains(REGEX_NUMBER_2)){
                            def index = expandString.indexOf(REGEX_NUMBER_2)
                            startOffset = index - expandOffset
                            endOffset = - ( expandString.length() + expandOffset - index )
                            needIndexFirst = false
                        }else if(expandString.contains(REGEX_NUMBER_3)){
                            def index = expandString.indexOf(REGEX_NUMBER_3)
                            startOffset = index - expandOffset
                            endOffset = - ( expandString.length() + expandOffset - index )
                            needIndexFirst = false
                        }else if(expandString.contains(REGEX_NUMBER_4)){
                            def index = text.indexOf(REGEX_NUMBER_4)
                            startOffset = index - expandOffset
                            endOffset = - ( text.length() + expandOffset - index )
                            needIndexFirst = false
                        }else if(expandString.contains(REGEX_NUMBER_5)){
                            def index = text.indexOf(REGEX_NUMBER_5)
                            startOffset = index - expandOffset
                            endOffset = - ( text.length() + expandOffset - index )
                            needIndexFirst = false
                        }

                        for (int j = 2; j < 1000; j++) {
                            def REGEX_1 = '。' + j + '.'
                            def REGEX_2 = '；' + j + '.'
                            def REGEX_3 = '。' + j + '、'
                            def REGEX_4 = '（' + j + '）'
//                            def REGEX_5 = '（' + j + ''
                            def expandOffset0 = 1
                            def replicationEndOffset = endOffset
                            while( expandOffset0 < 350 && rawText.length() > end + expandOffset0 + endOffset ){

                                def expandStr = ""
                                try {
                                    expandStr = rawText.substring(start,end+expandOffset0+endOffset)
                                } catch (Exception e) {
                                    e.printStackTrace()
                                }

                                if (needIndexFirst) {
                                    if(expandStr.contains(REGEX_NUMBER_1)){
                                        def index = expandStr.indexOf(REGEX_NUMBER_1)
                                        startOffset = index - expandOffset
                                        needIndexFirst = false
                                    }else if(expandStr.contains(REGEX_NUMBER_2)){
                                        def index = expandStr.indexOf(REGEX_NUMBER_2)
                                        startOffset = index - expandOffset
                                        needIndexFirst = false
                                    }else if(expandStr.contains(REGEX_NUMBER_3)){
                                        def index = expandStr.indexOf(REGEX_NUMBER_3)
                                        startOffset = index - expandOffset
                                        needIndexFirst = false
                                    }else if(expandString.contains(REGEX_NUMBER_4)){
                                        def index = text.indexOf(REGEX_NUMBER_4)
                                        startOffset = index - expandOffset
                                        endOffset = - ( text.length() + expandOffset - index )
                                        needIndexFirst = false
                                    }else if(expandString.contains(REGEX_NUMBER_5)){
                                        def index = text.indexOf(REGEX_NUMBER_5)
                                        startOffset = index - expandOffset
                                        endOffset = - ( text.length() + expandOffset - index )
                                        needIndexFirst = false
                                    }
                                }


                                if( expandStr =~ REGEX_1 || expandStr =~ REGEX_2
                                        || expandStr =~ REGEX_3 || expandStr =~ REGEX_4 ){
                                    endOffset += expandOffset0
                                    break
                                }
                                expandOffset0 ++
                            }
                            if(replicationEndOffset == endOffset){
                                break
                            }
                        }

                        def REGEX_STOP = "。"
                        def expand = 1
                        while( expand < 400 && rawText.length() > end + expand + endOffset ){
                            def expandStr = rawText.substring(end+endOffset,end+expand+endOffset)
                            if( expandStr =~ REGEX_STOP ){
                                endOffset += expand
                                break
                            }
                            expand ++
                        }

                        def REGEX_EXACT_STOP = '规定估值'
                        def exact_expand = 1
                        while( exact_expand < 2000 && rawText.length() > end + exact_expand + endOffset ){
                            def expandStr = rawText.substring(end+endOffset,end+exact_expand+endOffset)
                            if( expandStr =~ REGEX_EXACT_STOP ){
                                endOffset += exact_expand

                                break
                            }
                            exact_expand ++
                        }

                        def JSONTextMap = dependParams.JSONTextMap
                        def fileTextStr = ""
                        fileTextStr = JSONTextMap.get(drawResultVO.childTaskSn)
                        TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                        objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)
                        objectNew.text = rawText.substring( start + startOffset, end + endOffset )
                        objectNew.businessValue = rawText.substring( start + startOffset, end + endOffset )
                        trueValueSet.add(objectNew)
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }
        if(!CollectionUtils.isEmpty(resultVOs) && resultVOs.size()>2){
            trueValueSet.add(resultVOs[0])
            trueValueSet.add(resultVOs[1])
            return trueValueSet
        }
        return  resultVOs

    }

    static void main(String[] args) {

        def str = "。（2）"
        str = str.replaceAll("\n","")
        printf str.length().toString()

    }


}
